home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / LISP / GAMBIT_1 / OPEN_ME_.FIR < prev    next >
Text File  |  1992-03-18  |  15KB  |  460 lines

  1. Documentation for Gambit v1.8
  2. -----------------------------
  3.  
  4. The Gambit Scheme Interpreter is one component of the Gambit Scheme System.
  5. Also available are, a version with a native code compiler and a linker that
  6. can be used to build standalone applications (check the about... menu).
  7.  
  8. All of these applications run on Mac+/SE/II computers.  For the interpreter
  9. you need at least 850K of free RAM to run the program.  This means that
  10. it is possible to run it on a 1 meg Mac as long as you are not using some
  11. other RAM consuming programs, such as special INIT's, ramdisks or the
  12. Multifinder.  Chances are you won't have enough free space on a 1 meg Mac
  13. running system 7.0.
  14.  
  15.  
  16. Gambit Interpreter
  17. ------------------
  18.  
  19. The Gambit Interpreter (GSI for short) is a Scheme interpreter that
  20. can be used to develop and debug Gambit Scheme programs.  GSI conforms
  21. to the IEEE-Scheme standard as well as the R3.99RS report on Scheme.  In
  22. addition, GSI has the ability to dynamically load object files (i.e.
  23. files with a .O extension) produced by the Gambit compiler.  GSI's
  24. debugging environment supports program interrupt, error resume and abort.
  25. Gambit v1.8 implements the complete numeric tower defined in the standard,
  26. that is, exact integers of arbitrary precision (bignums), rationals,
  27. inexact reals (floating point) and complex numbers.  GSI also has a few
  28. extensions to the standard, for example, pretty printing routines and
  29. multitasking support.  Some of the Macintosh toolbox procedures
  30. are also available from the interpreter.
  31.  
  32. When GSI is launched, it grabs most of the memory for its heap and leaves
  33. the rest for the Mac's own needs.  Note that under Multifinder it is
  34. possible to limit the amount of memory used by GSI by changing GSI's
  35. memory requirement information with the finder's <cmd>-I command.
  36. Unfortunately, this means that on a small system (e.g. 1 meg Mac) there
  37. is a good chance that the Mac will run out of memory if you open too many
  38. windows or desk accessories... so be careful.  If you get an "Out of memory"
  39. dialog box, close some of your windows and DA's or delete some of the text
  40. in your interaction window.
  41.  
  42. If present, the file "init.scm" is loaded when GSI is launched.  It can
  43. be used to customize GSI to your own needs.  The file can be either
  44. in the same folder as the document being opened or in the same folder as
  45. the interpreter.
  46.  
  47.  
  48. Interaction with the Read-Eval-Print loop
  49. -----------------------------------------
  50.  
  51. Most of the interaction with GSI's read-eval-print (REP) loop is done
  52. through the keyboard.  To evaluate an expression, place the edit cursor
  53. after the end of the expression and press <enter>.  This will send the
  54. expression to the interpreter for evaluation and the result will be
  55. displayed in the interaction window.  In addition, it is possible to
  56. send arbitrary text to the interpreter by selecting it before pressing
  57. <enter>.  These evaluation mechanisms also work in windows attached to
  58. files (however the result is always displayed in the interaction
  59. window).  For example, if you are reading this file from GSI, place the
  60. edit cursor after the closing parenthesis of (* 3 5) and press <enter>.
  61. The value 15 will be printed on the interaction window.  The tab key can
  62. be used to indent the code.
  63.  
  64. In general, Scheme expressions typed in a REP loop are evaluated and
  65. the result printed out.  When an evaluation error occurs, a REP loop of
  66. a higher level is  entered (the topmost REP loop is numbered 0).  A <cmd>-D
  67. will cause the current REP loop to be aborted and the previous REP loop will be resumed.  As opposed to most other Lisp systems, GSI combines the standard REP
  68. loop functions with those of the debugger.   In a sense, you  are  always in
  69. debugging mode.  You can at all times examine the frames in the REP loop's
  70. continuation (i.e. the call stack).  This is useful to determine what part of
  71. the program signaled an error.  Also, an expression typed in to the REP loop
  72. will be evaluated in the context of the frame currently being examined.  Check
  73. the examples for details.  Special commands accepted in a REP loop are:
  74.  
  75. - <cmd>-R  Return from an error or interrupt (you must also supply a value).
  76. - <cmd>-D  Returns to the previous REP loop (also acts as end-of-file).
  77. - <cmd>-T  Returns to the top-most REP loop.
  78.  
  79. - <cmd>-H  Give summary of commands.
  80. - <cmd>-+  Move to next frame of the continuation. (<cmd>-= works also)
  81. - <cmd>--  Move to previous frame of the continuation.
  82.  
  83. - <cmd>-B  Display a summary of each frame in the continuation
  84.            starting with the current frame.  There are 3 columns.
  85.            The first is the frame number.  The second is the  procedure
  86.            that created the frame (and where control will return when
  87.            this frame is resumed).  The last column, if it is present,
  88.            is the expression whose value is being computed.  A frame
  89.            created by a compiled procedure has a third column only
  90.            if the procedure was compiled with the DEBUG option.
  91.  
  92. - <cmd>-L  List all non-global variables accessible from the current frame.
  93.  
  94. - <cmd>-P  Pretty print the procedure that created the current frame.
  95.  
  96. - <cmd>-E  Display subproblem expression for the current frame.
  97.  
  98. - <cmd>-.  This can be typed at any moment to interrupts the current
  99.            computation and starts a new REP loop.
  100.  
  101. - <cmd>-J  Load a file.
  102.  
  103.  
  104. Here is a transcript of a typical interaction with GSI (comments are
  105. marked with ";;;"):
  106.  
  107. Gambit (v1.8)
  108.  
  109. : (define (fact x)
  110.     (cond ((= x 0) 1)
  111.           ((= x 1) 1)
  112.           ((> x 1) (* (fact (- x 1)) x))
  113.           (else    (error "argument must be positive"))))
  114. fact
  115.  
  116. : (trace fact)  ;;; let's check what fact is doing
  117. fact
  118.  
  119. : (fact 10)
  120. Entry (fact 10)
  121. |Entry (fact 9)
  122. | Entry (fact 8)
  123. |  Entry (fact 7)
  124. |  |Entry (fact 6)
  125. |  | Entry (fact 5)
  126. |  |  Entry (fact 4)
  127. |  |  |Entry (fact 3)
  128. |  |  | Entry (fact 2)
  129. |  |  |  Entry (fact 1)
  130. |  |  |  ==> 1
  131. |  |  | ==> 2
  132. |  |  |==> 6
  133. |  |  ==> 24
  134. |  | ==> 120
  135. |  |==> 720
  136. |  ==> 5040
  137. | ==> 40320
  138. |==> 362880
  139. ==> 3628800
  140. 3628800
  141.  
  142. : (untrace fact)  ;;; remove tracing of fact
  143. fact
  144.  
  145. : (let ((x (runtime))) (let ((y (fact 50))) (cons (- (runtime) x) y)))
  146. (.02 . 30414093201713378043612608166064768844377641568960512000000000000)
  147.  
  148. : (pp fact)
  149. (lambda (x)
  150.   (cond ((= x 0) 1)
  151.         ((= x 1) 1)
  152.         ((> x 1) (* (fact (- x 1)) x))
  153.         (else (error "argument must be positive"))))
  154. #f
  155.  
  156. : (define f fact)   ;;; save value of fact in f
  157. f
  158.  
  159. : (define fact "not the factorial function")
  160. fact
  161.  
  162. : (f 5)
  163. *** ERROR IN f -- Operator is not a PROCEDURE
  164. (fact (- x 1))
  165.  
  166. 1:  ;;; typed: <cmd>-B
  167. 0    f                         (fact (- x 1))
  168. -1   (top level)               (f 5)
  169. -2   ##dynamic-env-bind
  170. -3   ##read-eval-print
  171. -4   ##dynamic-env-bind
  172. -5   ###_kernel.startup
  173.  
  174. 1:  ;;; typed: <cmd>-P
  175. #[procedure f] =
  176. (lambda (x)
  177.   (cond ((= x 0) 1)
  178.         ((= x 1) 1)
  179.         ((> x 1) (* (fact (- x 1)) x))
  180.         (else (error "argument must be positive"))))
  181.  
  182. 1:  ;;; typed: <cmd>-L
  183. x = 5
  184.  
  185. 1: (- x 1)   ;;; we can use x and it will refer to the local x (f's argument)
  186. 4
  187.  
  188. 1: fact   ;;; check value of fact
  189. "not the factorial function"
  190.  
  191. 1: (set! fact f)
  192. #[undefined]
  193.  
  194. 1:  ;;; typed: <cmd>-R  (we want to resume from the error)
  195. (fact (- x 1))   ;;; this is the value to return
  196. 120
  197.  
  198. : (list (gensym) (gensym) (gensym))
  199. (g1 g2 g3)
  200.  
  201. : (put 'bob 'eyes 'blue)
  202. #f
  203.  
  204. : (put 'mary 'hair 'blond)
  205. #f
  206.  
  207. : (get 'bob 'eyes)
  208. blue
  209.  
  210. : (get 'mary 'eyes)
  211. #f
  212.  
  213. : (define (series term)          ;;; Concurrency is expressed with FUTUREs
  214.     (let ((sum 0) (stop #f))     ;;; as is done in Multilisp
  215.       (FUTURE (let loop ((i 0))
  216.                 (if (not stop)
  217.                   (begin (set! sum (+ sum (term i))) (loop (+ i 1))))))
  218.       (lambda (msg)
  219.         (cond ((eq? msg 'value) sum)
  220.               ((eq? msg 'stop)  (set! stop #t))
  221.               (else             (error "unknown message" msg))))))
  222. series
  223.  
  224. : (define pi  ;;; start a task to compute series expansion for pi
  225.     (series (lambda (i) (/ 4. ((if (odd? i) - +) (+ (* i 2) 1))))))
  226. pi
  227.  
  228. : (pi 'value)   ;;; get current value of series
  229. 3.141419882340216
  230.  
  231. : (pi 'value)   ;;; again... it has changed!
  232. 3.1415194471477133
  233.  
  234. : (pi 'value)
  235. 3.1416300745380195
  236.  
  237. : (pi 'stop)   ;;; now we kill the task
  238. #[undefined]
  239.  
  240. : (pi 'value)
  241. 3.1415815090449892
  242.  
  243. : (pi 'value)
  244. 3.1415815090449892
  245.  
  246. : (load "pi")   ;;; try computing pi another way...
  247. How many digits of pi do you want (0 to exit): 50
  248. 3.14159265358979323846264338327950288419716939937510
  249. How many digits of pi do you want (0 to exit): 0
  250. "pi.scm"
  251.  
  252. : (queens 8 #t)   ;;; Ooops, queens is not defined...
  253. *** ERROR -- Unbound variable: queens
  254.  
  255. 1:   ;;; typed: <cmd>-D
  256. : (load "queens")   ;;; load it!
  257. "queens.O"
  258.  
  259. : (queens 8 #t)  ;;; try it out
  260. (4 2 7 3 6 8 5 1)
  261. (5 2 4 7 3 8 6 1)
  262. (3 6 4 2 8 5 7 1)
  263. (3 5 2 8 6 4 7 1)
  264. (5 7 1 3 8 6 4 2)
  265. (4 6 8 3 1 7 5 2)  ;;; typed: <cmd>-. to interrupt computation
  266.  
  267. *** INTERRUPT
  268.  
  269. 1:   ;;; typed: <cmd>-D
  270. : (load "tree")
  271. "tree.scm"
  272.  
  273. : (tree-display (call-with-input-file "fact.scm" read))
  274.               .                                             
  275.    .----------------------.                                 
  276.    .                      .                                 
  277. define     .-----------------------------.                  
  278.            .                             .                  
  279.          .---.         .-----------------------------------.
  280.          .   .         .                                   .
  281.        fact .--.  .----------.                            ()
  282.             .  .  .          .                              
  283.             n () if  .----------------.                     
  284.                      .                .                     
  285.                     .--.     .-----------------.            
  286.                     .  .     .                 .            
  287.                     < .--.   1        .-----------------.   
  288.                       .  .            .                 .   
  289.                       n .--.   .-------------.         ()   
  290.                         .  .   .             .              
  291.                         2 ()   *       .-----------.        
  292.                                        .           .        
  293.                                    .-------.      .--.      
  294.                                    .       .      .  .      
  295.                                  fact  .--------. n ()      
  296.                                        .        .           
  297.                                       .--.     ()           
  298.                                       .  .                  
  299.                                       - .--.                
  300.                                         .  .                
  301.                                         n .--.              
  302.                                           .  .              
  303.                                           1 ()              
  304. #[undefined]
  305.  
  306. :   ;;; typed: <cmd>-Q
  307.  
  308.  
  309.  
  310. Non-standard procedures
  311. -----------------------
  312.  
  313. There are a few non-standard, but very useful, procedures available in
  314. this version of GSI:
  315.  
  316.  
  317. (pretty-print obj port width)    Writes out the object 'obj' on 'port' in a
  318.                                  nice way.  Port defaults to the current
  319.                                  output port and width defaults to 79.
  320.  
  321. (pp obj port width)              Same as 'pretty-print' but writes out the
  322.                                  source code of 'obj' if it is a procedure and
  323.                                  the source code is available.
  324.  
  325. (gensym prefix)                  Generates a unique (non-interned) symbol with
  326.                                  a given prefix.  Prefix can be a string or
  327.                                  symbol, and defaults to "g".
  328.  
  329. (get sym prop)                   Return the value associated with property
  330.                                  'prop' of symbol 'sym'.  If no such property
  331.                                  exists, returns #f.
  332.  
  333. (put sym prop val)               Associates the value 'val' with property
  334.                                  'prop' of symbol 'sym'.
  335.  
  336. (runtime)                        Returns the number of elapsed seconds since
  337.                                  the program started.
  338.  
  339. (error msg args...)              Prints out the message and arguments, and
  340.                                  invokes the debugger.
  341.  
  342. (eval expr)                      Evaluates the expression expr in the global
  343.                                  environment, for example (eval '(+ 1 2)) => 3.
  344.  
  345. (set-gc-report flag)             If flag is true, every garbage collection
  346.                                  will print out some GC statistics.
  347.  
  348.  
  349. There are also a number of toolbox routines available from Scheme.  However,
  350. these procedures do not check the validity of their arguments.  It is easy to
  351. crash the system by passing an illegal value... be careful.  A few examples
  352. of these procedures are given in "mac-examples.scm".
  353.  
  354. ;;; Quickdraw stuff
  355.  
  356. ; points
  357.  
  358. (mac#point v h) ; to allocate points
  359. (mac#point-v pt)
  360. (mac#point-h pt)
  361. (mac#point-v-set! pt x)
  362. (mac#point-h-set! pt x)
  363.  
  364. ; rectangles
  365.  
  366. (mac#rect top left bottom right) ; to allocate rectangles
  367. (mac#rect-top r)
  368. (mac#rect-left r)
  369. (mac#rect-bottom r)
  370. (mac#rect-right r)
  371. (mac#rect-top-set! r x)
  372. (mac#rect-left-set! r x)
  373. (mac#rect-bottom-set! r x)
  374. (mac#rect-right-set! r x)
  375.  
  376. ; procedures (check inside Mac for meaning of arguments)
  377.  
  378. (mac#openport port)
  379. (mac#initport port)
  380. (mac#closeport port)
  381. (mac#setport port)
  382. (mac#getport)
  383. (mac#hidecursor)
  384. (mac#showcursor)
  385. (mac#pensize width height)
  386. (mac#penmode mode)
  387. (mac#penpat pat)
  388. (mac#pennormal)
  389. (mac#moveto h v)
  390. (mac#move dh dv)
  391. (mac#lineto h v)
  392. (mac#line dh dv)
  393. (mac#textfont font)
  394. (mac#textface face)
  395. (mac#textmode mode)
  396. (mac#textsize size)
  397. (mac#drawchar ch)
  398. (mac#drawstring s)
  399. (mac#charwidth ch)
  400. (mac#stringwidth s)
  401. (mac#localtoglobal pt)
  402. (mac#globaltolocal pt)
  403. (mac#framerect r)
  404. (mac#paintrect r)
  405. (mac#eraserect r)
  406. (mac#invertrect r)
  407. (mac#fillrect r pat)
  408. (mac#frameroundrect r ovwd ovht)
  409. (mac#paintroundrect r ovwd ovht)
  410. (mac#eraseroundrect r ovwd ovht)
  411. (mac#invertroundrect r ovwd ovht)
  412. (mac#fillroundrect r ovwd ovht pat)
  413. (mac#frameoval r)
  414. (mac#paintoval r)
  415. (mac#eraseoval r)
  416. (mac#invertoval r)
  417. (mac#filloval r pat)
  418. (mac#framearc r startangle arcangle)
  419. (mac#paintarc r startangle arcangle)
  420. (mac#erasearc r startangle arcangle)
  421. (mac#invertarc r startangle arcangle)
  422. (mac#fillarc r startangle arcangle pat)
  423.  
  424. ;;; Other toolbox stuff
  425.  
  426. (mac#getmouse pt)
  427. (mac#button)
  428. (mac#sysbeep duration)
  429.  
  430. ; events
  431.  
  432. (mac#event-what ev)
  433. (mac#event-message ev)
  434. (mac#event-when ev)
  435. (mac#event-where ev)
  436. (mac#event-modifiers ev)
  437.  
  438. ; standard file get/put
  439.  
  440. (mac#sfgetfile prompt ftypes)  ; prompt and ftypes are optional
  441. (mac#sfputfile prompt default) ; prompt and default are optional
  442.  
  443. ; low-level windows
  444.  
  445. (mac#newwindow bounds title goaway)
  446. (mac#disposewindow window)
  447.  
  448. ;;; Special hooks to Gambit's user interface
  449.  
  450. ; editor windows
  451.  
  452. (mac#edit filename line char) ; pops up a window to edit the given file and
  453.                               ; position cursor to the coordinates.  line and
  454.                               ; char default to 0.
  455.  
  456. ; interaction windows
  457.  
  458. (mac#open-window name) ; create a window that behaves like a Scheme port.
  459.                        ; the port is both an input and output port.
  460.